package com.matchmealpha;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class FindFriendsActivity extends Activity implements OnClickListener {

	private Button leftButton;
	private Button rightButton;
	private TextView errorMsg;
	private EditText searchText;
	private Button findFriendsButton;

	private ListView findFriendsLayout;
	private List<String> findFriendsList;
	private FindFriendsListAdapter findFriendsAdapter;

	private String username;
	private String sessionid;

	protected ProgressDialog loadingDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/*** Shared Pref ***/
		SharedPreferences settings = getSharedPreferences(
				"matchme_preferences", 0);
		username = settings.getString("username", "");
		sessionid = settings.getString("sessionid", "");

		/*** Init ***/
		initLayout();
		initListener();

		findFriendsList = MatchMeTabWidget.findFriendsList;
		
		if(findFriendsList == null)
		{
			MatchMeTabWidget.findFriendsList = new ArrayList<String>();
			findFriendsList = MatchMeTabWidget.findFriendsList;
			
		}
		
		findFriendsAdapter = new FindFriendsListAdapter(this, findFriendsList,
				sessionid);
		findFriendsLayout.setAdapter(findFriendsAdapter);
		

	}

	private void initLayout() {

		setContentView(R.layout.findfriends_view);

		findFriendsButton = (Button) findViewById(R.id.FindFriendsButton);
		leftButton = (Button) this.findViewById(R.id.FindFriendsLeftButton);
		rightButton = (Button) this.findViewById(R.id.FindFriendsRightButton);
		searchText = (EditText) this.findViewById(R.id.FindFriendsEditText);
		findFriendsLayout = (ListView) this
				.findViewById(R.id.FindFriendListView);
		errorMsg = (TextView) this.findViewById(R.id.FindFriendsError);
		errorMsg.setVisibility(View.GONE);
		rightButton.setVisibility(View.INVISIBLE);
	}

	private void displayLayout() {
		errorMsg.setVisibility(View.GONE);
		searchText.setVisibility(View.VISIBLE);
		findFriendsLayout.setVisibility(View.VISIBLE);
		findFriendsButton.setVisibility(View.VISIBLE);
		findFriendsAdapter.notifyDataSetChanged();
		errorMsg.postInvalidate();
	}

	private void displayErrorLayout() {
		errorMsg.setVisibility(View.VISIBLE);
		searchText.setVisibility(View.INVISIBLE);
		findFriendsLayout.setVisibility(View.INVISIBLE);
		findFriendsButton.setVisibility(View.INVISIBLE);
		findFriendsAdapter.notifyDataSetChanged();
		errorMsg.postInvalidate();
	}

	private void initListener() {
		leftButton.setOnClickListener(this);
		findFriendsButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.FindFriendsButton:
			String itemText = searchText.getText().toString();
			InputMethodManager inputManager = (InputMethodManager) this
					.getSystemService(Context.INPUT_METHOD_SERVICE);
			inputManager.hideSoftInputFromWindow(this.getCurrentFocus()
					.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			if (!itemText.equals("")) {
				loadingDialog = ProgressDialog.show(getParent(), "",
						"Searching...", true);
				new FindFriendsTask().execute();
			}
			break;
		case R.id.FindFriendsLeftButton:
			InputMethodManager inputManager2 = (InputMethodManager) this
					.getSystemService(Context.INPUT_METHOD_SERVICE);
			inputManager2.hideSoftInputFromWindow(this.getCurrentFocus()
					.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			MatchMeTabWidget.myBack(3);
			break;
		default:
			break;
		}
	}

	private class FindFriendsTask extends AsyncTask<String, Void, Boolean> {

		protected Boolean doInBackground(String... data) {

			HttpRequest req = new HttpRequest(sessionid);
			String response;
			response = req.sendPost(MatchMeTabWidget.MM_SERVER
					+ "/find_friends/" + searchText.getText().toString(), "");
			if (MatchMeTabWidget.USER_AUTH_FAILURE.equals(response))
				return false;
			else if (response != "") {
				Scanner in = new Scanner(response).useDelimiter(";");
				String str;
				findFriendsList.clear();
				while (in.hasNext()) {
					str = in.next();
					findFriendsList.add(str);
				}
				return true;
			} else
				return false;
		}

		protected void onPostExecute(Boolean result) {
			loadingDialog.cancel();
			if (result) {
				if (findFriendsList.size() <= 0) {
					Toast.makeText(getParent(), "Cannot find a match",
							Toast.LENGTH_LONG);
				}
				displayLayout();
			} else {
				errorMsg.setText("Unable to connect. Please try again");
				displayErrorLayout();
			}
		}
	}
}
